package us.gaaoc.framework.webapp.backingbeans.attorney;

import java.util.List;
import java.util.Map;

import javax.faces.model.DataModel;
import javax.faces.model.ListDataModel;

import us.gaaoc.framework.model.CaseRecordHistory;
import us.gaaoc.framework.webapp.backingbeans.BaseBackingBean;


/**
 * This class is the backing bean for CaseRecordHistoryPage.xhtml. It provides
 * the user with the requested case record history data table and allows
 * navigation to the previous page.
 * 
 * @author Brandon Denney
 * @date May 29, 2008
 * @version GAJE 2.x
 */
public class CaseRecordHistoryPage extends BaseBackingBean {
	
	/* **************** *
	 * Global Variables *
	 * **************** */
	
	/** Serial Version UID */
	private static final long serialVersionUID = 2893472L;
	/**	Data model for the requested case record history. */
	private DataModel caseRecordHistoryDataTable;	
	@SuppressWarnings("unchecked")
	/** Global session map */
	private Map sessionMap;	
	
	/* ******* *
	 * Getters *
	 * ******* */
	/**
	 * Getter for the caseRecordHistoryDataTable data model.
	 * 
	 * @return the data model containing the case record history.
	 */
	public DataModel getCaseRecordHistoryDataTable() {
		return caseRecordHistoryDataTable;
	}
	
	/**
	 * Getter for the sessionMap map.
	 * 
	 * @return the current session map.
	 */
	@SuppressWarnings("unchecked")
	public Map getSessionMap() {
		sessionMap = this.getFacesContext().getExternalContext()
			.getSessionMap();
		
		return sessionMap;
	}
	
	/* ******* *
	 * Setters *
	 * ******* */
	/**
	 * Setter for the caseRecordHistoryDataTable data model.
	 * 
	 * @param caseRecordHistoryDataTable the data model to contain the case
	 * record history.
	 */
	public void setCaseRecordHistoryDataTable(DataModel caseRecordHistoryDataTable) {
		this.caseRecordHistoryDataTable = caseRecordHistoryDataTable;
	}
	
	/**
	 * Setter for the sessionMap map.
	 * 
	 * @param sessionMap the map to replace the current session map.
	 */
	@SuppressWarnings("unchecked")
	public void setSessionMap(Map sessionMap) {
		this.sessionMap = sessionMap;
	}
	
	/**
	 * Default constructor.  Calls a method to populate the data table with
	 * the requested case record history. 
	 */
	public CaseRecordHistoryPage() {
		
		/*
		 * This try/catch block prevents the page from breaking if the id
		 * does not load properly.
		 */
		try{
			this.loadData();
		} catch (Exception e) {
			back();
		}
	}
	
	/**
	 * Method used to populate the data table with the requested case history.
	 */
	@SuppressWarnings("unchecked")
	public void loadData() {
		Map sessionMap = this.getSessionMap();
		String caseUuid = (String)sessionMap.get("caseUuid");
		String courtUuid = (String) sessionMap.get("courtUuid");
		
		List<CaseRecordHistory> historyList = this.getModelFacade()
			.findHistoryByCaseRecordID(caseUuid, courtUuid);
		
		caseRecordHistoryDataTable = new ListDataModel();
		caseRecordHistoryDataTable.setWrappedData(historyList);
	}
	
	/**
	 * Method used to navigate the user to the previous page.
	 * 
	 * @return the navigation string to send the user to the previous page.
	 */
	@SuppressWarnings("unchecked")
	public String back() {
		
		Map sessionMap = this.getSessionMap();
		String fromPage = (String) sessionMap.get("from");
		sessionMap.remove("from");
		sessionMap.remove("caseUuid");
		if (fromPage.equalsIgnoreCase("attFilingsTablesPage"))
			return "allFilingsPage"; 
		if(fromPage.equalsIgnoreCase("searchPage")) {
			sessionMap.put("from", "attFilingsTablesPage");
			return "searchPage";
		} else
		return "back";
	}
}
